﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.View;
using ERP.Hoadon;
using ERP.Khachhang;
using ERP.Reports;

namespace ERP.Hoadon
{
    public partial class viewErp_Nhaphoadon_Tructiep : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
        public cs_erp_hanghoaInfo hh = new cs_erp_hanghoaInfo();
        int count = 0;
        public int flag = 1;
        int cFlag = 1;

        public viewErp_Nhaphoadon_Tructiep()
        {
            InitializeComponent();
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            viewERP_Khachhang kh = new viewERP_Khachhang();
            kh.user = this.user;
            kh.flag = 1;
            kh.count = 1;
            kh.ShowDialog();
            if (kh.count == 2)
            {
                this.kh = kh.khachhang;
                this.Load_Info();
                this.cFlag = 2;
                //this.Load_Data();
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            viewErp_Ql_Khachhang kh = new viewErp_Ql_Khachhang();
            kh.user = this.user;
            kh.ShowDialog();
            if (kh.flag == 4)
            {
                this.kh = kh.khachhang;
                this.Load_Info();
                this.cFlag = 2;
                //this.Load_Data();
            }
        }

        private void Save_Info()
        {
            this.hoadon.id_kh = this.kh.id_kh;
            this.hoadon.sohd = this.txtHoadon.Text.Trim();
            this.hoadon.ngayban = Convert.ToDateTime(this.txtNgayban.EditValue.ToString().Trim());
            //this.hoadon.ngayhentra = Convert.ToDateTime(this.txtNgayhen.EditValue.ToString().Trim());
            //this.hoadon.ngaythu = Convert.ToDateTime(this.txtNgaythu.EditValue.ToString().Trim());
            this.hoadon.nguoiban = this.user.id_nv;
            this.hoadon.khuyenmai = Convert.ToDouble(this.txtKhuyenmai.Value.ToString().Trim());
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.hoadon.trangthai = 1;
            this.hoadon.tructiep = true;
            this.btnLuu.Enabled = false;
            double tongtien = 0;
            for (int i = 0; i < this.count; i++)
            {
                tongtien += Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                    Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim())
                    + Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                    Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) *
                    Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
            }
            if (this.cFlag == 1)
            {
                XtraMessageBox.Show("Bạn chưa chọn khách hàng, bạn phải chọn khách hàng trước khi lưu hóa đơn", "Thông báo");
                this.btnLuu.Enabled = true;
                return;
            }
            this.Save_Info();
            this.hoadon.tongtien = tongtien;
            this.hoadon.conlai = this.hoadon.tongtien - this.hoadon.khuyenmai - this.hoadon.datra;
            if (this.hoadon.ExcuteProcedure(this.flag) > 0)
            {
                for (int i = 0; i < this.count; i++)
                {
                    this.hoadon.Get_HD();
                    if (Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) > 0)
                    {
                        cs_erp_chitiet_hoadonInfo ct = new cs_erp_chitiet_hoadonInfo();
                        ct.id_hoadon = this.hoadon.id_hd;
                        ct.id_monan = Convert.ToInt32(this.gridView1.GetRowCellValue(i, grdcID).ToString().Trim());
                        ct.soluong = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim());
                        ct.dongia = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim())
                            + Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                                Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
                        ct.thanhtien = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                                Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim())
                            + Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                                Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) *
                                Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
                        if (ct.ExcuteProcedure(1) <= 0)
                        {
                            ct.ExcuteProcedure(2);
                        }
                    }
                }
                //Sau khi lưu thành công hóa đơn, thực hiện lưu thanh toán tiền cọc
                //Neu la sua doi hoa don thi khong cho thay doi tien dat coc
                if (this.flag != 2)
                {
                    cs_erp_hoadon_thanhtoanInfo tt = new cs_erp_hoadon_thanhtoanInfo();
                    tt.id_hoadon = this.hoadon.id_hd;
                    tt.ngaythanhtoan = this.hoadon.ngayban;
                    tt.nguoinhantien = this.user.id_nv;
                    tt.lanthanhtoan = 1;
                    tt.sotien = Convert.ToDouble(this.txtTiencoc.Value.ToString().Trim());
                    tt.ExcuteProcedure(1);
                }
                XtraMessageBox.Show("Lưu hóa đơn thành công!", "Thông báo");
                this.inHoadon();
            }
        }

        private void viewErp_Nhaphoadon_Tructiep_Load(object sender, EventArgs e)
        {
            if (this.flag == 1)
            {
                this.Load_Data();
                this.Init();
            }
            else
            {
                if (this.flag == 3)// Chi xem
                {
                    this.kh.id_kh = this.hoadon.id_kh;
                    this.kh.Get_Info();
                    this.Load_Info();
                    this.Load_HD_Info();
                    this.Load_Data();
                    this.btnLuu.Enabled = false;
                    this.cFlag = 2;
                }
                else //Dieu chinh
                {
                    this.kh.id_kh = this.hoadon.id_kh;
                    this.kh.Get_Info();
                    this.Load_Info();
                    this.Load_HD_Info();
                    this.Load_Data();
                    this.cFlag = 2;
                }
            }
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }

        private void Load_Info()
        {
            this.txtHoten.Text = this.kh.tenkhachhang;
            this.txtDiachi.Text = this.kh.diachi;
            this.txtDienthoai.Text = this.kh.dienthoai;
        }

        private void Load_HD_Info()
        {
            this.txtHoadon.Text = this.hoadon.sohd;
            this.txtNgayban.EditValue = this.hoadon.ngayban;
            //this.txtNgayhen.EditValue = this.hoadon.ngayhentra;
            //this.txtNgaythu.EditValue = this.hoadon.ngaythu;
            this.txtKhuyenmai.EditValue = Convert.ToDecimal(this.hoadon.khuyenmai);
            //this.txtTiencoc.EditValue = Convert.ToDecimal(this.hoadon)
            cs_erp_hoadon_thanhtoanInfo tt = new cs_erp_hoadon_thanhtoanInfo();
            tt.id_hoadon = this.hoadon.id_hd;
            this.txtTiencoc.EditValue = Convert.ToDecimal(tt.Get_Sum());
            this.txtTongcong.EditValue = Convert.ToDecimal(this.hoadon.tongtien);
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            ds = this.hh.Get_List_by_HD_TT(this.hoadon.id_hd);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void Init()
        {
            this.txtHoadon.Text = "BHMS:" + DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "-" + this.user.username;
            this.txtNgayban.Text = DateTime.Now.ToShortDateString();
            //this.txtNgayhen.Text = DateTime.Now.AddDays(7).ToShortDateString();
            //this.txtNgaythu.Text = DateTime.Now.AddDays(5).ToShortDateString();
            this.txtTiencoc.Value = 0;
        }

        private void inHoadon()
        {
            DataSet ds = new DataSet("ds");
            string ban1 = this.kh.tenkhachhang + ", Ngày: " + DateTime.Now.ToShortDateString();
            cs_erp_chitiet_hoadonInfo ct = new cs_erp_chitiet_hoadonInfo();
            ct.id_hoadon = this.hoadon.id_hd;
            ds = ct.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("Không có số liệu...", "Thông báo!");
                return;
            }
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.captren = "0";
            dv.Get_Info_by_Parents();
            rptDoanhthu rpt = new rptDoanhthu();
            rpt.DataSource = ds.Tables[0];
            rpt.ds_BaoCao = ds.Tables[0];
            rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
            rpt.ban = ban1;
            rpt.donvi = dv.tendonvi;
            rpt.diachi = dv.diachi + " - " + dv.sodt_fax;
            rpt.In();
            rpt.ShowPreview();
            //rpt.e

            //rpt.CreateDocument(true);
            //viewReport frm = new viewReport();
            ////rpt.PrintingSystem = frm.printingSystem1;
            //frm.dt_In = ds.Tables[0];
            //frm.ban = ban1;
            //frm.int_BaoCao = 1;
            //frm.rpt = rpt;

            ////frm.MdiParent = this.MdiParent;
            //frm.ShowDialog();
        }

        private void grcDanhsach_Leave(object sender, EventArgs e)
        {
            try
            {
                double tongtien = 0;
                for (int i = 0; i < this.count; i++)
                {
                    tongtien += Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                        Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim())
                        + Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                        Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) *
                        Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
                }
                this.txtTongcong.Value = Convert.ToDecimal(tongtien);
            }
            catch
            {
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void grcDanhsach_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                double tongtien = 0;
                for (int i = 0; i < this.count; i++)
                {
                    tongtien += Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                        Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim())
                        + Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim()) *
                        Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) *
                        Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn1).ToString().Trim());
                }
                this.txtTongcong.Value = Convert.ToDecimal(tongtien);
            }
            catch
            {
            }
        }
    }
}